package com.tuanzhang.zhw.rpc.spring;


import com.tuanzhang.zhw.rpc.config.consumer.RpcClientConfig;
import com.tuanzhang.zhw.rpc.register.RpcRegistryConsumerService;
import com.tuanzhang.zhw.rpc.register.zookeeper.CuratorImpl;
import com.tuanzhang.zhw.rpc.register.zookeeper.RpcRegistryProviderService;
import com.tuanzhang.zhw.rpc.register.zookeeper.ZookeeperClient;
import com.tuanzhang.zhw.rpc.spring.annotaion.RpcScan;
import com.tuanzhang.zhw.rpc.spring.annotaion.RpcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.stereotype.Component;

/**
 * rpc 加载class类 注册器
 */
@Slf4j
public class RpcClassPathScannerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware {

    private ResourceLoader resourceLoader;
    private Environment environment;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {

        // 1、解析注解获取 扫描目录
        AnnotationAttributes annotationAttributes = AnnotationAttributes.fromMap(annotationMetadata.getAnnotationAttributes(RpcScan.class.getName()));
        String[] basePackages = annotationAttributes.getStringArray("basePackage");
        if (basePackages == null || basePackages.length == 0) {
            basePackages = new String[]{((StandardAnnotationMetadata) annotationMetadata).getIntrospectedClass().getPackage().getName()};
        }
        //设置解析 注解
        RpcClassPathScanner rpcClassPathScannerServer = new RpcClassPathScanner(beanDefinitionRegistry, RpcService.class);
        RpcClassPathScanner rpcClassPathScannerComponent = new RpcClassPathScanner(beanDefinitionRegistry, Component.class);
        rpcClassPathScannerServer.setResourceLoader(this.resourceLoader);
        rpcClassPathScannerComponent.setResourceLoader(this.resourceLoader);

        int springBeanAmount = rpcClassPathScannerComponent.scan("com.tuanzhang.zhw.rpc.spring");
        log.info("【zhw-rpc】springBeanScanner扫描的数量 [{}]", springBeanAmount);
        int rpcServiceCount = rpcClassPathScannerServer.scan(basePackages);
        log.info("【zhw-rpc】Scanner扫描的数量 [{}]", rpcServiceCount);

    }


}
